package org.luxor.common.netty.protocol;

import io.netty.util.internal.StringUtil;

/**
 * 固定消息头
 *
 * @author Mr.yan @date 2024/1/31
 */
public class NettyFixedHeader {

    /**
     * 消息类型
     */
    private final NettyMessageType messageType;

    /**
     * 重发标志<p>
     * false：表示这是客户端或服务端第一次请求发送这个报文
     * true：表示这可能是一个早前报文请求的重发
     */
    private final boolean isDup;

    /**
     * 消息服务质量类型<p>
     * 0：不保证消息送达
     * 1：确保消息至少送达1次(接收方必须回复ACK应答消息，否则会重复发送12次，直到收到ack应答)
     * 2：确保消息只送达1次
     */
    private final NettyQoS qosLevel;

    private final int remainingLength;

    public NettyFixedHeader(NettyMessageType messageType, boolean isDup, NettyQoS qosLevel) {
        this(messageType,isDup,qosLevel,0);
    }

    public NettyFixedHeader(NettyMessageType messageType, boolean isDup, NettyQoS qosLevel, int remainingLength) {
        this.messageType = messageType;
        this.isDup = isDup;
        this.qosLevel = qosLevel;
        this.remainingLength = remainingLength;
    }

    public NettyMessageType messageType() {
        return messageType;
    }

    public boolean isDup() {
        return isDup;
    }

    public NettyQoS qosLevel() {
        return qosLevel;
    }

    public int remainingLength() {
        return remainingLength;
    }

    @Override
    public String toString() {
        return StringUtil.simpleClassName(this) +
                '[' +
                "messageType=" + messageType +
                ", isDup=" + isDup +
                ", qosLevel=" + qosLevel +
                ", remainingLength=" + remainingLength +
                ']';
    }
}
